package org.ysh.demo;

import org.apache.commons.io.IOUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.web.client.RestTemplate;
import org.ysh.demo.mapper.UsersMapper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;

@SpringBootApplication
public class ShiroApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ShiroApplication.class, args);
//        UsersMapper mapper = context.getBean(UsersMapper.class);
//        org.ysh.demo.domain.Users user = mapper.findByUsername("闫帅");
//        System.out.println(user.getUsername());
//        downloadFile(context.getBean(RestTemplate.class));
        getToken(context.getBean(RestTemplate.class));
    }

    public static void getToken(RestTemplate restTemplate){
        String ME_PATH = "http://localhost:8082/shiro/archive/getAppCode";
        String appId = "e0eca445e3430bc86eeef0be28252b9a94abb8ed873031a62d36ab2aa3ab6abc";
        String appSecret= "2a19c60cc359b8e9798f4b01aaf41256b5018f4445ecd48b2193a7dd4820a07c";
        String code = "6b2d8045abb582fc2a7fe10126225ff65a1e48e87753659453428b95ed467e47";
        String state = System.currentTimeMillis() + "";

        String url = "http://localhost:8080/darj/api/oauth/companyToken?appId=" + appId + "&appSecret=" + appSecret + "&code=" + code + "&redirectUri=" + ME_PATH + "&state=" + state;

        ResponseEntity<Map> mapResponseEntity = restTemplate.postForEntity(url, null, Map.class, Collections.EMPTY_MAP);
        //这个结果Map中会包含companyToken及companyId等信息
        System.out.println(mapResponseEntity.getBody().entrySet());
    }

    public static void downloadFile(RestTemplate restTemplate) {
        String time = System.currentTimeMillis()+ "";
        String id = "a08184868ff05396018ff09c13a40001105151";
        String secret = "goodnews";
        String signature = "";
//        String url ="http://szzx-dangan.skynj.com/JS-CPPCC-OA/report/upLoad?id="+id+"&time="+time+"&signature=xxxxxx";
//        String url = "http://szzx-dangan.skynj.com/JS-CPPCC-OA/report/upLoad?id=293232&time=1487654343222&signature=XsdfwFF";
        //http://szzx-dangan.skynj.com/JS-CPPCC-OA/report/upLoad?id=293232&time=1487654343222&signature=XsdfwFF
        String url = "http://szzx-dangan.skynj.com/JS-CPPCC-OA/report/upLoad?id=a08184868ff05396018ff09c13a40001105151&time=1718354286880&signature=gxqWHVqdTo4vIY5mVwIyYA==";
        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET,null,byte[].class, Collections.EMPTY_MAP);
        byte[] fileBytes = responseEntity.getBody();
        byte[] decode = Base64Utils.decode(fileBytes);
        try(FileOutputStream fos = new FileOutputStream("D:\\3333333.txt")){
            IOUtils.write(decode,fos);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
